home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 101-125 / disk_111 / assigndev / assigndev.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  178 lines

  1. /****************************************************************************
  2. * A S S I G N D E V . C
  3. *
  4. *   Duplicate a device in place--Allowing multiple names to a device.
  5. *
  6. *   Phillip Lindsay (c) 1987 Commodore-Amiga, Inc.
  7. *   Unlimited use granted as long as copyright remains intact.
  8. *
  9. ****************************************************************************/
  10.  
  11.                                                               /* V23.08.87 */
  12. /* Some changes and a bug fix were made by Olaf Seibert, KosmoSoft */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/nodes.h>
  16. /* #include <exec/lists.h> */
  17. #include <exec/memory.h>
  18. #include <libraries/dos.h>
  19. #include <libraries/dosextens.h>
  20. #include <libraries/filehandler.h>
  21.  
  22.  
  23. #ifdef AZTEC_C
  24. #include <functions.h>
  25. #else
  26.     *** WARNING: YOU SHOULD BEST BE USING AZTEC C TO COMPILE THIS CODE ***
  27. #endif
  28.  
  29. extern  struct  DosLibrary      *DOSBase;
  30.  
  31.  
  32. /*
  33.  * external functions found in "misc.c"
  34.  */
  35. extern  BSTR    ctob();
  36. extern  char    *allocstr(),
  37.                 *btoc(),
  38.                 *strupper();
  39. extern  BOOL    ourdevnode();
  40. extern  struct DeviceNode
  41.                 *finddosnode();
  42. extern  VOID    freedev(),
  43.                 dupdev(),
  44.                 adddosnode(),
  45.                 trunc(),
  46.                 memfill(),
  47.                 memcpy();
  48.  
  49. /*
  50.  * External functions found in "fprints.c"
  51.  */
  52. extern  int     fprints(),
  53.                 fputs();
  54.                 
  55. /*
  56.  * Exit message numbers
  57.  */
  58.  
  59. #define ERROR_NOT_FOUND   0
  60. #define ERROR_NOT_REMOVED 1
  61. #define ERROR_REMOVED     2
  62. #define ERROR_COPIED      3
  63.  
  64. static char TxtError[]  = "Error: ";
  65.  
  66. main(argc,argv)
  67. int argc;
  68. char **argv;
  69. {
  70.                 struct  RootNode    *rnode;     /* root node pointer     */
  71.                 struct  DosInfo     *dinfo;     /* DOS info pointer      */
  72.     register    struct  DeviceNode  *dnode;     /* device entry pointer  */
  73.     register            BPTR        *dnodelink; /* last device pointer   */
  74.                         char        *bname,     /* tmp. ptr to bstr      */
  75.                                     name[32],   /* tmp. for device name  */
  76.                                     *oldname,   /* existing device name  */
  77.                                     *newname;   /* new device name       */
  78.                         int         status;     /* error flag            */
  79.                 struct  FileHandle  *stdout = Output();
  80.  
  81.     if (argc == 3) { /* duplicate given device  */
  82.         newname = argv[1];
  83.         trunc(strupper(newname),':');
  84.         
  85.         if ( dnode = finddosnode(newname) ) { /* OIS V09.08.87 */
  86.             static char exists[] = " already exists";
  87.             if (dnode->dn_Type == DLT_DEVICE) {
  88.                 fprints(stdout, "Warning: Device %s:%s; ", newname, exists);
  89.             } else {
  90.                 fprints(stdout, "%sVolume or directory %s:%s\n",
  91.                     TxtError, newname, exists);
  92.                 exit((int) RETURN_ERROR);
  93.             }
  94.         }
  95.         
  96.         oldname = argv[2];
  97.         trunc(strupper(oldname),':');
  98.  
  99.     } else if (argc == 2) { /* Try and remove specified device */
  100.         oldname = argv[1];
  101.         trunc(strupper(oldname),':');
  102.         newname = NULL;
  103.     } else { /* Give them usage info */ 
  104.         if (argc != 0) { /* Started from a CLI */ /* OIS V23.08.87 */
  105.             static char old[] = " [old device]\n";
  106.             fprints(stdout, "Usage: %s [new device]%s", argv[0], old);
  107.             fprints(stdout, "       %s%s", argv[0], old);
  108.         }
  109.         exit((int) RETURN_ERROR);
  110.     }        
  111.     
  112.    
  113.     rnode   = (struct RootNode *)  DOSBase->dl_Root;        /* Find root node */
  114.     dinfo   = (struct DosInfo  *)  BADDR(rnode->rn_Info);   /* Now dos info   */
  115.     dnodelink =                    &dinfo->di_DevInfo;      /* Our first link */
  116.  
  117. /*
  118.  * Here we search for the given device name and once found we will duplicate
  119.  * the entire device data structure.  Then we add a new device node with
  120.  * the supplied name to the device list. If no "newname" is specified the
  121.  * given device is removed from the device list and free memory for the device
  122.  * node and device name. (normally you only delete nodes you created...I do 
  123.  * take precautions to NOT delete existing system devices...I add a key to
  124.  * the end of the device name to flag the device node as being created by
  125.  * ASSIGNDEV. 
  126.  */
  127.     
  128.     Forbid();          /* Protect ourselves */
  129.     
  130.     while (dnode = (struct DeviceNode *) BADDR(*dnodelink)) {
  131.         if ( dnode->dn_Type == DLT_DEVICE )  /* Are dealing with a device? */
  132.         {
  133.             bname = (char *) BADDR(dnode->dn_Name);
  134.             memcpy(bname, name, (ULONG)( bname[0] + 1 ) );
  135.             if ( !( strcmp( oldname, strupper(btoc(name)) ) ) ) {
  136.                 if ( newname ) {                     /* Duplicate it ? */
  137.                     dupdev(dnode,newname);
  138.                     status = ERROR_COPIED;
  139.                 } else if ( ourdevnode(dnode) ) {    /* OIS V15.08.87 */
  140.                     *dnodelink = dnode->dn_Next;     /* No, remove it */
  141.                     freedev(dnode);
  142.                     status = ERROR_REMOVED;
  143.                 } else {
  144.                     status = ERROR_NOT_REMOVED;
  145.                 }                                    /* OIS V09.08.87 */
  146.                 break;
  147.             }   
  148.         }
  149.         dnodelink = &dnode->dn_Next;
  150.     }
  151.  
  152.     Permit(); 
  153.  
  154.     switch (status) { /* OIS V09.08.87 */
  155.     case ERROR_NOT_FOUND:
  156.         fprints(stdout, "%sDevice %s: not found\n", TxtError, oldname);
  157.         status = RETURN_ERROR;
  158.         break;
  159.     case ERROR_NOT_REMOVED:
  160.         fprints(stdout, "%sCannot remove device %s:\n", TxtError, oldname); 
  161.         status = RETURN_ERROR;
  162.         break;
  163.     case ERROR_REMOVED:
  164.         fprints(stdout, "Removed %s:\n",oldname);
  165.         status = RETURN_OK;
  166.         break;
  167.     case ERROR_COPIED:
  168.         fprints(stdout, "Duplicated %s: -> %s:\n",oldname,newname);
  169.         status = RETURN_OK;
  170.     }
  171.  
  172.     exit(status);
  173.      
  174. }
  175.  
  176. /* eof */
  177.